package org.example.managementsystem.service;

import org.example.managementsystem.model.Provider;
import org.example.managementsystem.model.ProviderApplication;
import org.example.managementsystem.model.ProviderApplicationReview;
import org.example.managementsystem.repository.ProviderApplicationDao;
import org.example.managementsystem.repository.ProviderApplicationReviewDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 供应商申请审核服务层
 * @author 王振祎
 * @date 2025-05-21
 */
@Service
public class ProviderApplicationReviewService {

    private final ProviderApplicationReviewDao providerApplicationReviewDao;
    private final ProviderApplicationDao  providerApplicationDao;
    private final ProviderService providerService;

    @Autowired
    public ProviderApplicationReviewService(ProviderApplicationDao providerApplicationDao, ProviderApplicationReviewDao providerApplicationReviewDao, ProviderService providerService) {
        this.providerApplicationDao = providerApplicationDao;
        this.providerApplicationReviewDao = providerApplicationReviewDao;
        this.providerService = providerService;
    }

    /**
     * 审核供应商申请
     */
    public void reviewApplication(ProviderApplicationReview review) {

        // 查询供应商审核，若有对应的审核记录，则更新
        ProviderApplicationReview existingReview = providerApplicationReviewDao.getReviewByApplicationId(review.getApplicationId());
        if(existingReview == null) {
            providerApplicationReviewDao.updateReview(review);
        }
        else {
            providerApplicationReviewDao.insertReview(review);
        }

        //更新申请表的状态
        providerApplicationDao.updateApplicationStatus(review.getApplicationId(), review.isApproved(), true);

        // 审批通过后，将供应商加入供应商列表和数据库中
        if(review.isApproved() == true) {
            ProviderApplication providerApplication = new ProviderApplication();
            Provider provider = new Provider();
            providerApplication = providerApplicationDao.getApplicationById(review.getApplicationId());
            provider.setProviderName(providerApplication.getProviderName());
            provider.setFactoryAddress(providerApplication.getFactoryAddress());
            provider.setManagerName(providerApplication.getManagerName());
            provider.setManagerPhone(providerApplication.getManagerPhone());
            provider.setCompanyType(providerApplication.getCompanyType());
            provider.setRegisteredCapital(providerApplication.getRegisteredCapital());
            provider.setEmployeeCount(providerApplication.getEmployeeCount());
            provider.setEstablishedYear(providerApplication.getEstablishedYear());
            provider.setProfile(providerApplication.getProfile());
            provider.setProviderRating(review.getInitialRating());
            providerService.addProvider(provider);
        }


    }

    /**
     * 获取所有供应商申请审核记录
     */
    public List<ProviderApplicationReview> getAllReviews() {
        return providerApplicationReviewDao.getAllReviews();
    }

    /**
     * 根据申请ID获取供应商申请审核记录
     */
    public ProviderApplicationReview getReviewById(int reviewId) {
        return providerApplicationReviewDao.getReviewByApplicationId(reviewId);
    }

}
